package main

import (
	"embed"
	"github.com/wailsapp/wails/v2"
	"github.com/wailsapp/wails/v2/pkg/options"
	"github.com/wailsapp/wails/v2/pkg/options/assetserver"
	"ideatools/application/core/entry"
	"ideatools/application/db/sqllite"
	"ideatools/application/first"
	"ideatools/application/pkgs/filehelper"
	"ideatools/application/pkgs/mylog"
	"ideatools/application/pkgs/utils"
	"ideatools/application/pkgs/yaml"
	"ideatools/setting"
	"math"
	"path"
	"path/filepath"
)

//go:embed all:frontend/dist
var assets embed.FS

func main() {
	// Create an instance of the app structure
	app := NewApp()

	//首次启动
	initDbData()

	//一个两个配置：1.数据库sqlLite--config表  2.本config.yaml文件
	//初始化yaml 本地配置，本地配置不会上传到云端
	yaml.InitConfig(filepath.Join(utils.GetUserHomeDir(), setting.ProjectName, "config.yaml"))
	first.SetYamlFirstValue()

	//初始化sqlLitePath
	sqlLitePath := filepath.Join(utils.GetUserHomeDir(), setting.ProjectName, "data.db")
	err := sqllite.InitSqlLite(sqlLitePath)
	if err != nil {
		panic(err.Error())
	}

	//日志初始化
	LogDir := path.Join(utils.GetUserHomeDir(), setting.ProjectName, "logs")
	if !filehelper.FileExists(LogDir) {
		filehelper.CreateDir(LogDir)
	}
	mylog.SetLogPath(LogDir)
	mylog.Info("程序启动")

	//初始化service
	entry.InitService(setting.ProjectName)

	//获取初始高度
	w, h := GetStartSize(55, 80)

	err = wails.Run(&options.App{
		Title:  setting.WindowsTitle, // 设置应用程序标题
		Width:  w,                    // 设定初始宽度为 888 像素
		Height: h,                    // 设定初始高度为 666 像素
		//Menu:   mymenu.GetMenu(), //自定义菜单
		AssetServer: &assetserver.Options{ // 设置资源服务器相关选项
			Assets: assets, // 指定应用程序需要加载的静态资源
		},

		//Frameless:        true, //无边框
		//WindowStartState: options.Maximised, // 初始窗口状态为最大化窗口
		//Frameless:        true,                      // 设定窗口是否无边框模式
		BackgroundColour: &options.RGBA{ // 设定应用程序背景颜色
			R: 27,
			G: 38,
			B: 54,
			A: 1,
		},
		OnBeforeClose: app.beforeClose,
		OnStartup:     app.startup, // 在启动过程中执行 app.startup 函数
		Bind: []interface{}{ // 绑定 Go 类型及其方法供 JavaScript 调用
			app, // 绑定 app 对象
			//service.NewFileConfigService(ProjectName), //配置读写--数文件版
			entry.ClipDbService, //前铁板

			entry.SystemService,   //系统相关
			entry.UserToolService, //工具相关
			entry.ConfigService,   //配置相关
			entry.KmListenService, //鼠标相关
			//entry.KeyboardService, //鍵盤相關
			entry.ClipService,       //剪贴板服务
			entry.BackService,       //后台服务
			entry.BackService,       //后台服务
			entry.ChatDialogService, //后台服务
			//entry.LangService,           //语言包服务
			entry.HandlerMessageService, //核心消息处理业务--gpt相关
		},
	})

	if err != nil {
		println("Error:", err.Error())
	}
}

// GetStartSize 获取界面的初始宽高
// PerW 宽度百分比，取值1-100
// PerW 高度百分比 ，取值1-100
func GetStartSize(PerW, PerH float64) (width int, height int) {
	w, h, err := utils.GetScreenSize()
	if err != nil {
		return 888, 666
	}

	width = int(math.Floor(float64(w) * PerW * 0.01))  //宽度占比
	height = int(math.Floor(float64(h) * PerH * 0.01)) //高度占比
	return width, height
}
